package com.sunchen.asc.service.item.server;


import com.sunchen.asc.service.common.ResultResponse;
import com.sunchen.asc.service.common.ResultUtil;
import com.sunchen.asc.service.item.api.TitleService;
import com.sunchen.asc.service.item.domain.Title;
import com.sunchen.asc.service.item.repository.TitleRepository;
import com.sunchen.asc.service.item.vo.TitleVo;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;

import java.sql.Timestamp;
import java.util.List;
import java.util.stream.Collectors;

@DubboService(retries = 0)
public class TitleServiceImpl implements TitleService {

    @Autowired
    TitleRepository titleRepository;

    public ResultResponse createTitle(TitleVo titleVo) {

        Long count = titleRepository.countByNameAndStatus(titleVo.getName(),1);
        if (count > 0 ){
            return ResultUtil.failed("该名称已存在");
        }

        Title title = new Title(titleVo);
        if (titleVo.getParentId() == null){
            title.setParentId(0L);
            title.setSortOrder(0L);
        }else {
            Long countSort = titleRepository.countByParentIdAndStatus(titleVo.getParentId(),1);
            title.setSortOrder(countSort + 1);
        }
        title.setStatus(1);
        //title.setCreateUser();
        titleRepository.save(title);
        return ResultUtil.success("1");
    }

    public ResultResponse editTitle(TitleVo titleVo) {

        Title title = titleRepository.getOne(titleVo.getId());
        if (!title.getName().equals(titleVo.getName())){
            Long count = titleRepository.countByNameAndStatus(titleVo.getName(),1);
            if (count > 0 ){
                return ResultUtil.failed("该名称已存在");
            }
        }
         title.setName(titleVo.getName());
        title.setIcon(titleVo.getIcon());
        title.setType(titleVo.getType());
        title.setOrgCode(titleVo.getOrgCode());
        title.setRegionCode(titleVo.getRegionCode());
        title.setRegionName(titleVo.getRegionName());
        title.setRemark(titleVo.getRemark());
        title.setClassType(titleVo.getClassType());
        //title.setUpdateUser();
        titleRepository.save(title);

        return ResultUtil.success("1");
    }

    public ResultResponse deleteTitle(Long id) {

        Title title = titleRepository.getOne(id);
        Long count = titleRepository.countByParentIdAndStatus(id,1);
        if (count > 0){
            return ResultUtil.failed("存在子主题，无法删除");
        }
        title.setStatus(0);
        title.setDeleteTime(new Timestamp(System.currentTimeMillis()));
        //title.setDeleteUser();
        titleRepository.save(title);

        return ResultUtil.success("1");
    }

    public ResultResponse list() {

        List<TitleVo> vos = titleRepository.findAllVo();
        return ResultUtil.success(vos);
    }

    public ResultResponse showByType(Integer titleType) {

        List<Title> titles = titleRepository.findAllByTitleTypeAndStatus(titleType,1);
        List<TitleVo> vos = titles.stream().map(title -> {
            TitleVo vo = new TitleVo();
            BeanUtils.copyProperties(vo,title);
            return vo;
        }).collect(Collectors.toList());
        return ResultUtil.success(vos);
    }


}
